package com.suke.czx.modules.project.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.ProjectSaveGroup;
import com.suke.czx.common.validate.ProjectUpdateGroup;
import com.suke.czx.modules.project.entity.Project;
import com.suke.czx.modules.project.entity.ProjectSearchBo;
import com.suke.czx.modules.project.service.ProjectService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 项目
 * 代码描述: 项目控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/07 10:15:53
 */
@Validated
@RestController
@RequestMapping("/api/project")
@AllArgsConstructor
@Api(value = "ProjectController", tags = "项目管理")
public class ProjectController extends AbstractController {

    private final ProjectService projectService;

    /**
     * 分页查询项目列表
     * @param projectSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:project:list')")
    public R list(ProjectSearchBo projectSearchBo) {
        if (projectSearchBo.getPage() == null || projectSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询信息列表
        return projectService.pageProjectList(projectSearchBo);
    }

    /**
     * 保存项目
     * @param project 项目
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存项目")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:project:save')")
    public R save(@Validated(ProjectSaveGroup.class) Project project, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        project.setCreator(getUserId());
        project.setCreateTime(new Date());
        projectService.save(project);
        return R.ok();
    }

    /**
     * 更新项目
     * @param project 项目
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新项目")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:project:update')")
    public R update(@Validated(ProjectUpdateGroup.class) Project project, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        project.setUpdater(getUserId());
        project.setUpdateTime(new Date());
        projectService.updateById(project);
        return R.ok();
    }

    /**
     * 更新项目状态
     * @param projectId 项目编码
     * @return VO
     */
    @SysLog("更新项目状态")
    @RequestMapping(value = "/status", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:project:status')")
    public R status(@NotNull(message = "项目编码不能为空") Long projectId, @NotNull(message = "项目状态不能为空") Integer status) {
        return projectService.updateProjectStatus(projectId, status, getUserId());
    }

}